package com.yft.dao.login;

import com.yft.dao.base.BaseConstant;
import com.yft.dao.base.BaseDao;
import com.yft.entity.base.BaseEntityUtils;
import com.yft.entity.base.Order;
import com.yft.entity.base.Pager;
import com.yft.entity.login.SysMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


@Service("sysMemberDao")
public class SysMemberDao {
	
	@Autowired(required = true)
	@Qualifier(BaseConstant.DAO_NAME)
	private BaseDao dao;
	
	public Object addSysMember(SysMember sysMember) {
		return dao.addEntity(sysMember);
	}
	
	public SysMember getSysMember(Object sysMemberPK){
		return (SysMember)dao.getEntity(SysMember.class,(Serializable)sysMemberPK);
	}
	
	@SuppressWarnings("unchecked")
	public SysMember getSysMemberByLoginId(String loginId){
		String q = "select c from SysMember c where c.loginID=?";
		List<SysMember> sysMemberList = (List<SysMember>)dao.getHibernateTemplate().find(q,loginId);
		return sysMemberList==null || sysMemberList.isEmpty() ? null :sysMemberList.get(0);
	}
	
	public void updateLoginInfo(Integer memberId,String loginIP){
		String q = "update SysMember set loginSum=loginSum+1, lastLoginTime=nowLoginTime,nowLoginTime=current_timestamp(), lastLoginIp='"+loginIP+"' where sysMemberId="+memberId;
		dao.executeHql(q);
	}
	
	public boolean updateValues(SysMember condition,String customStr){
		return dao.updateValues(condition, customStr);
	}
	
	@SuppressWarnings("unchecked")
	public List<SysMember> getSysMemberList(SysMember condition, Order order,Pager pager){
		return getList("",condition,order,pager);
	}	
	
	@SuppressWarnings("unchecked")
	private List getList(String hql,SysMember condition,Order order,Pager pager){
		
		String q = hql+"from SysMember c  where 1=1 ";
		q += makeCondition(condition);
		q += order!=null? order.toString():"";
		
		return dao.getListPager(q,condition,pager);
		
	}
	
	public String makeCondition(SysMember condition){
		condition = condition==null? new SysMember():condition;
		
		String q = "";
		q += condition.getLoginID()!=null? " and loginID = :loginID":"";
		
		q += BaseEntityUtils.makeBaseCondition(condition);
		
		return q;
	}
}
